import dayjs from "dayjs";

export const timeConverts = (timestamp: number) => {
  if (!timestamp) return "";
  let now_timestamp = +new Date();
  let diff_timestamp = now_timestamp - timestamp;
  if (diff_timestamp / 1000 <= 0) return `0秒前`;
  if (diff_timestamp / 1000 <= 60) return `${Math.floor(diff_timestamp / 1000)}秒前`;
  if (diff_timestamp / 1000 / 60 <= 60) return `${Math.floor(diff_timestamp / 1000 / 60)}分钟前`;
  if (diff_timestamp / 1000 / 60 / 60 <= 24) return `${Math.floor(diff_timestamp / 1000 / 60 / 60)}小时前`;
  return `${Math.floor(diff_timestamp / 1000 / 60 / 60 / 24)}天前`;
};

export const unitConverts = (value: number) => {
  if (value >= 1000000) return `${(value / 1000000).toFixed(2)}M`;
  if (value >= 1000) return `${(value / 1000).toFixed(2)}K`;
  return `${value}`;
};

export const formatDate = (value: number) => {
  if (!value) return "--";
  return dayjs(value).format("YYYY-MM-DD HH:mm:ss");
};

export const formatTime = (seconds: any) => {
  const days = Math.floor(seconds / 86400);
  const hours = Math.floor((seconds % 86400) / 3600);
  const minutes = Math.floor((seconds % 3600) / 60);
  const secs = seconds % 60;

  return `${days}天${hours}小时${minutes}分钟${secs}秒`;
};
